package open.panxiong.shared.http.controller

import android.content.Context
import fi.iki.elonen.NanoHTTPD.IHTTPSession
import fi.iki.elonen.NanoHTTPD.Response
import open.panxiong.shared.http.annotation.NanohttpdApi
import open.panxiong.shared.http.config.ConstConfig
import open.panxiong.shared.http.model.NanohttpdResponse

/**
 * 默认的API接口对象
 */
class DefaultController {

    // 检查文件是否有更新
    @NanohttpdApi("/api/check-update-state")
    fun checkUpdateState(context: Context, session: IHTTPSession): Response {
        val files = ConstConfig.getShareFileList(context)
        if (files.isEmpty()) {
            return NanohttpdResponse(data = 0L).toJson()
        }
        val time = files.last().lastModified()  // 返回最新时间，客户端自己比对是否需要更新
        return NanohttpdResponse(data = time).toJson()
    }

}